#include <bits/types/clock_t.h>

#include <ctime>
#include <memory>
#include <thread>

#include "CommonConnectionPool.h"
#include "Connection.h"

// 压力测试连接池的使用效果

int main() {
    Connection conn("localhost", 3306, "root", "123456", "chat");
    conn.update("delete from user");

    // 不使用连接池
    clock_t begin1 = clock();
    auto worker = []() {
        for (int i = 0; i < 100; ++i) {
            Connection conn("localhost", 3306, "root", "123456", "chat");
            conn.update(
                "insert into user(name, age, sex) values('alice', 1, 'male')");
        }
    };
    std::thread t1(worker);
    std::thread t2(worker);
    std::thread t3(worker);
    std::thread t4(worker);
    std::thread t5(worker);

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    clock_t end1 = clock();
    std::cout << "不使用连接池：" << (end1 - begin1) / CLOCKS_PER_SEC
              << " seconds" << std::endl;

    // 使用连接池
    conn.update("delete from user");
    clock_t begin2 = clock();
    auto worker2 = []() {
        ConnectionPool *connectionPool = ConnectionPool::getInstance();
        for (int i = 0; i < 100; ++i) {
            std::shared_ptr<Connection> conn = connectionPool->getConnection();
            conn->update(
                "insert into user(name, age, sex) values('alice', 1, 'male')");
        }
    };
    std::thread tt1(worker2);
    std::thread tt2(worker2);
    std::thread tt3(worker2);
    std::thread tt4(worker2);
    std::thread tt5(worker2);

    tt1.join();
    tt2.join();
    tt3.join();
    tt4.join();
    tt5.join();
    clock_t end2 = clock();
    std::cout << "使用连接池：" << (end2 - begin2) / CLOCKS_PER_SEC
              << " seconds" << std::endl;
    return 0;
}
